#include "RingQueue.hpp"
#include "task.hpp"
#include <ctime>
#include <pthread.h>
#include <memory>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>

using namespace std;
const char* ops = "+-*/%";

void* consumerRoutine(void* args)
{
    RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        rq->pop(&t);
        t();
        cout<<"consumer done,处理完成的任务是"<<t.formatRes()<<endl;
    } 
}
void* productorRoutine(void* args)
{
    RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        sleep(1);
        int x = rand()%100;
        int y = rand()%100;
        char op = ops[(x+y)%strlen(ops)];
        Task t(x,y,op);
        rq->push(t);
        cout<<"productorRoutine done , 生产的任务:"<<t.formatArg()<<endl;
    }
}
int main()
{
    srand(time(nullptr)^getpid());

    RingQueue<Task>* rq = new RingQueue<Task>();
    pthread_t c,p;
    pthread_create(&c,nullptr,consumerRoutine,rq);
    pthread_create(&p,nullptr,productorRoutine,rq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);


    return 0;
}